package othello.gui;

import othello.model.IBoard;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;

public abstract class IGui {
    private static IGui _instance;

    public static IGui get() {
        return _instance;
    }

    /**
     * Whomever extends from us and instantiates an instance becomes the global
     * IGui available via the get() method.
     */
    protected IGui() {
        _instance = this;
    }

    /**
     * Tells the GUI which board's state to display.
     */
    public abstract void setBoard(IBoard board);

    /**
     * @return the frame coordinates of the given square.
     */
    public abstract Rectangle getSquare(int row, int col);

    /**
     * @return the row/col (as Point.y/.x) of a square, given a coordinate on
     * the frame.  Return null if the coordinate is off the board.
     */
    public abstract Point getSquare(Point pt);

    /**
     * @return true iff the given coordinate is over the PASS button.
     */
    public abstract boolean isPassButton(Point pt);

    /**
     * Instructs the GUI to update the image at the earliest convenience.
     */
    public abstract void redrawBoard();

    /**
     * Attaches the listener to the main frame.
     * @param listener may be a KeyListener, MouseListener, MouseMotionListener,
     * or any combination of the above.
     */
    public abstract void addListener(Object listener);

    /**
     * Detaches a listener that was previously attached above.
     */
    public abstract void removeListener(Object listener);

    /**
     * Requests that the GUI indicate a potential piece placement.
     * A special code of color==null && row==max_rows means that the
     * pass button should be highlighted.
     */
    public abstract void showDropIndicator(Color color, int row, int col);

    public static String colorName(Color c) {
        if (c.equals(Color.BLACK)) {
            return "Black";
        }
        if (c.equals(Color.WHITE)) {
            return "White";
        }
        if (c.equals(Color.RED)) {
            return "Red";
        }
        if (c.equals(Color.ORANGE)) {
            return "Orange";
        }
        if (c.equals(Color.YELLOW)) {
            return "Yellow";
        }
        if (c.equals(Color.GREEN)) {
            return "Green";
        }
        if (c.equals(Color.BLUE)) {
            return "Blue";
        }
        if (c.equals(Color.PINK)) {
            return "Pink";
        }
        if (c.equals(Color.CYAN)) {
            return "Cyan";
        }
        if (c.equals(Color.GRAY)) {
            return "Gray";
        }
        if (c.equals(Color.DARK_GRAY)) // I discourage using black, because it ruins the 3D look.
        {
            return "Black";				// So I call this black.
        }
        if (c.equals(Color.LIGHT_GRAY)) {
            return "Silver";
        }
        if (c.equals(Color.MAGENTA)) {
            return "Magenta";
        }

        float[] hsb = new float[3];
        Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsb);
        if (hsb[2] > 0.5) {
            return "Light";
        } else {
            return "Dark";
        }
    }
}
